package com.ruoyi.zsyh.controller.applet.store;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * 服务订单 -- 全部订单
 */
@Controller
@RequestMapping("/applet/zsyh/serviceOrderRepair")
public class ZsyhServiceOrderRepairAppletController extends BaseController
{

    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhUserRepairareaService zsyhUserRepairareaService;

    @Autowired
    private IZsyhServiceOrderPartsService zsyhServiceOrderPartsService;

    @Autowired
    private IZsyhServiceOrderPartsRelService zsyhServiceOrderPartsRelService;

    @Autowired
    private IZsyhServiceOrderMessageService zsyhServiceOrderMessageService;

    @Autowired
    private IZsyhServiceOrderApplymoneyService zsyhServiceOrderApplymoneyService;

    @Autowired
    private IZsyhServiceOrderAppointmentService zsyhServiceOrderAppointmentService;

    @Autowired
    private IZsyhServiceOrderLogService zsyhServiceOrderLogService;

    @Autowired
    private IZsyhServiceOrderSettlementService zsyhServiceOrderSettlementService;

    @Autowired
    private IZsyhAfterCommissionSettingService zsyhAfterCommissionSettingService;

    @Autowired
    private IZsyhAfterFactoryService zsyhAfterFactoryService;

    @Autowired
    private IZsyhPersonOrderService zsyhPersonOrderService;


    /**
     * 维修师傅 订单中心 各个状态的数字
     * @return
     */
    @RequestMapping("/getServiceOrderNum")
    @ResponseBody
    public AjaxResult getServiceOrderNum(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id

        ZsyhUserRepairarea area = new ZsyhUserRepairarea();
        area.setRepairId(repairId);
        List<ZsyhUserRepairarea> areaList = zsyhUserRepairareaService.selectZsyhUserRepairareaList(area);
        String areaIds = "";
        for(ZsyhUserRepairarea a:areaList){
            areaIds += a.getRepairDistrict() + ",";
        }

        //orderState 订单状态 0未接单（厂家下单）1已接单 101已预约 102服务中 2已完成 201平台待审核 202厂家待审核 203待拨款 3已支付 4已评价 5已取消 501师傅申请取消 6系统自动取消
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setOrderState("0");
        Map params = new HashMap();
        params.put("areaIds", areaIds);
        order.setParams(params);

        List<ZsyhServiceOrder> orderList1 = zsyhServiceOrderService.selectZsyhServiceOrderList(order);//新工单

        order = new ZsyhServiceOrder();
        order.setRepairStoreId(repairId);
        order.setOrderState("1");
        List<ZsyhServiceOrder> orderList2 = zsyhServiceOrderService.selectZsyhServiceOrderList(order);//待预约订单

        order = new ZsyhServiceOrder();
        order.setRepairStoreId(repairId);
        order.setOrderState("101");
        List<ZsyhServiceOrder> orderList3 = zsyhServiceOrderService.selectZsyhServiceOrderList(order);//待上门订单

        order = new ZsyhServiceOrder();
        order.setRepairStoreId(repairId);
        order.setOrderState("102");
        List<ZsyhServiceOrder> orderList4 = zsyhServiceOrderService.selectZsyhServiceOrderList(order);//服务中订单

        order = new ZsyhServiceOrder();
        order.setRepairStoreId(repairId);
        order.setOrderState("2");
        List<ZsyhServiceOrder> orderList5 = zsyhServiceOrderService.selectZsyhServiceOrderList(order);//已完成订单

        ZsyhServiceOrderParts parts = new ZsyhServiceOrderParts();
        parts.setRepairId(repairId);
        parts.setPartsState("1");
        List<ZsyhServiceOrderParts> partsList = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsList(parts);

        ZsyhServiceOrderApplymoney applymoney = new ZsyhServiceOrderApplymoney();
        applymoney.setRepairId(repairId);
        applymoney.setApplyState("1");
        List<ZsyhServiceOrderApplymoney> applyMoneyList = zsyhServiceOrderApplymoneyService.selectZsyhServiceOrderApplymoneyList(applymoney);

        ZsyhServiceOrderMessage message = new ZsyhServiceOrderMessage();
        message.setIsRead("0");
        Map map = new HashMap();
        map.put("repairId", repairId);
        map.put("repairNotRed", "repairNotRed");//查询未读消息
        message.setParams(map);

        List<ZsyhServiceOrderMessage> messageList = zsyhServiceOrderMessageService.selectZsyhServiceOrderMessageList(message);

        //个人单子 订单状态 0未接单 1已接单 2已完成 3已支付 4已评价 5已取消 6系统自动取消
        ZsyhPersonOrder personOrder = new ZsyhPersonOrder();
        personOrder.setOrderState("0");
        Map params1 = new HashMap();
        params1.put("areaIds", areaIds);
        personOrder.setParams(params1);

        List<ZsyhPersonOrder> personOrderList1 = zsyhPersonOrderService.selectZsyhPersonOrderList(personOrder);//新工单

        personOrder = new ZsyhPersonOrder();
        personOrder.setRepairStoreId(repairId);
        personOrder.setOrderState("1");
        List<ZsyhPersonOrder> personOrderList2 = zsyhPersonOrderService.selectZsyhPersonOrderList(personOrder);//待预约订单

        personOrder = new ZsyhPersonOrder();
        personOrder.setRepairStoreId(repairId);
        personOrder.setOrderState("101");
        List<ZsyhPersonOrder> personOrderList3 = zsyhPersonOrderService.selectZsyhPersonOrderList(personOrder);//待上门订单

        personOrder = new ZsyhPersonOrder();
        personOrder.setRepairStoreId(repairId);
        personOrder.setOrderState("102");
        List<ZsyhPersonOrder> personOrderList4 = zsyhPersonOrderService.selectZsyhPersonOrderList(personOrder);//服务中订单

        personOrder = new ZsyhPersonOrder();
        personOrder.setRepairStoreId(repairId);
        personOrder.setOrderState("2");
        List<ZsyhPersonOrder> personOrderList5 = zsyhPersonOrderService.selectZsyhPersonOrderList(personOrder);//已完成订单



        map = new HashedMap();
        map.put("orderListSize1", orderList1.size() + personOrderList1.size());//新工单
        map.put("orderListSize2", orderList2.size() + personOrderList2.size());//待预约订单
        map.put("orderListSize3", orderList3.size() + personOrderList3.size());//待上门订单
        map.put("orderListSize4", orderList4.size() + personOrderList4.size());//服务中订单
        map.put("orderListSize5", orderList5.size() + personOrderList5.size());//已完成订单

        map.put("partsListSize", partsList.size());
        map.put("applyMoneyListSize", applyMoneyList.size());
        map.put("messageListSize", messageList.size());

        return success("成功", map);
    }

    /**
     * 维修师傅 订单中心 各个订单状态列表
     * @param orderState 0新工单 1待预约 101待上门 102服务中 2已完成 203待结算 3已结算
     * 订单状态 001厂家保存（未下单） 0未接单（厂家下单）1已接单 101已预约 102服务中  201平台待审核 202厂家待审核
     *                   2已完成 203待拨款 3已支付 4已评价 5已取消 501师傅申请取消
     * @return
     */
    @RequestMapping("/getServiceOrderList")
    @ResponseBody
    public AjaxResult getServiceOrderList(String userId, String orderState, String pageNum, String pageSize, String key)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id

        //orderState 订单状态 0未接单（厂家下单）1已接单 101已预约 102服务中 2已完成 201平台待审核 202厂家待审核 203待拨款 3已支付 4已评价 5已取消 501师傅申请取消 6系统自动取消
        Map map = new HashedMap();
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        if("3".equals(orderState)){
            map.put("orderState", "已结算");
        }else{
            order.setOrderState(orderState);
        }

        if(!"0".equals(orderState)){
            order.setRepairStoreId(repairId);
        }else{
            ZsyhUserRepairarea area = new ZsyhUserRepairarea();
            area.setRepairId(repairId);
            List<ZsyhUserRepairarea> areaList = zsyhUserRepairareaService.selectZsyhUserRepairareaList(area);
            String areaIds = "";
            for(ZsyhUserRepairarea a:areaList){
                areaIds += a.getRepairDistrict() + ",";
            }
            map.put("areaIds", areaIds);
        }

        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("key", key);
        if("203".equals(orderState) || "3".equals(orderState)){
            order.setOrderType("2");//待结算，已结算的只查询厂家售后单
        }
        order.setParams(map);
        List<ZsyhServiceOrder> orderList = zsyhServiceOrderService.selectZsyhServiceOrderList(order);//首页订单列表

        return success("成功", orderList);
    }


    /**
     * 维修师傅 订单中心 订单详情
     * @return
     */
    @RequestMapping("/getServiceOrderDetails")
    @ResponseBody
    public AjaxResult getServiceOrderDetails(String userId, String orderId)throws Exception
    {
        //主订单
        ZsyhServiceOrder order = zsyhServiceOrderService.selectZsyhServiceOrderById(orderId);

        //订单关联的配件单
        ZsyhServiceOrderParts parts = new ZsyhServiceOrderParts();
        parts.setOrderId(orderId);
        List<ZsyhServiceOrderParts> partsList = zsyhServiceOrderPartsService.selectZsyhServiceOrderPartsList(parts);

        //订单关联的配件单
        ZsyhServiceOrderApplymoney applymoney = new ZsyhServiceOrderApplymoney();
        applymoney.setOrderId(orderId);
        List<ZsyhServiceOrderApplymoney> applyMoneyList = zsyhServiceOrderApplymoneyService.selectZsyhServiceOrderApplymoneyList(applymoney);

        //订单操作日志
        ZsyhServiceOrderLog log = new ZsyhServiceOrderLog();
        log.setOrderId(orderId);
        List<ZsyhServiceOrderLog> logList = zsyhServiceOrderLogService.selectZsyhServiceOrderLogList(log);

        //工单费用信息
        ZsyhServiceOrderSettlement settlement = new ZsyhServiceOrderSettlement();
        settlement.setOrderId(orderId);
        settlement.setFeeType("2");
        List<ZsyhServiceOrderSettlement> settlementList = zsyhServiceOrderSettlementService.selectZsyhServiceOrderSettlementList(settlement);

        //判断厂家是否允许使用自购件 是否使用自购件 1是 0否
        String factoryId = order.getFactoryId();
        order.setIsSelfParts("0");
        if(EmptyUtils.isNotEmpty(factoryId)){
            ZsyhAfterFactory factory = zsyhAfterFactoryService.selectZsyhAfterFactoryById(factoryId);
            if(factory != null){
                order.setIsSelfParts(factory.getIsSelfParts());
            }
        }

        ZsyhServiceOrderApplymoney applymoney1 = new ZsyhServiceOrderApplymoney();
        applymoney1.setOrderId(orderId);
        applymoney1.setApplyType("1");
        List<ZsyhServiceOrderApplymoney> applymoney1List = zsyhServiceOrderApplymoneyService.selectZsyhServiceOrderApplymoneyList(applymoney1);
        if(applymoney1List.size() > 0){//已经申请过里程费
            order.setIsMileageFee("1");
        }else{//还没有申请过里程费
            order.setIsMileageFee("0");
        }

        order.setPartsList(partsList);
        order.setApplyMoneyList(applyMoneyList);
        order.setLogList(logList);
        order.setSettlementList(settlementList);

        return success("成功", order);
    }

    /**
     * 维修师傅 订单中心 预约客户
     * @return
     */
    @RepeatSubmit
    @Transactional
    @RequestMapping("/appointmentServiceOrder")
    @ResponseBody
    public AjaxResult appointmentServiceOrder(String userId, String orderId, String appointmentDate, String appointmentContent)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id

        ZsyhServiceOrderAppointment appointment = new ZsyhServiceOrderAppointment();
        appointment.setId(IdWorker.getId());
        appointment.setOrderId(orderId);
        appointment.setRepairId(repairId);
        appointment.setAppointmentDate(appointmentDate);
        appointment.setAppointmentContent(appointmentContent);
        zsyhServiceOrderAppointmentService.insertZsyhServiceOrderAppointment(appointment);

        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setId(orderId);
        order.setOrderState("101");
        order.setAppointmentTime(new Date());
        zsyhServiceOrderService.updateZsyhServiceOrder(order);

        ZsyhServiceOrderLog log = new ZsyhServiceOrderLog();
        log.setId(IdWorker.getId());
        log.setOrderId(orderId);
        log.setLogType("1");
        log.setLogContent("师傅和客户预约，预约时间：" + appointmentDate + "。");
        zsyhServiceOrderLogService.insertZsyhServiceOrderLog(log);

        return success("成功");
    }

    /**
     * 维修师傅 订单中心 取消订单申请
     * @return
     * @throws Exception
     */
    @RequestMapping("/cancelServiceOrder")
    @ResponseBody
    public AjaxResult cancelServiceOrder(String orderId, String userId, String cancelRemark)throws Exception
    {
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setId(orderId);
        order.setOrderState("501");
        order.setCancelRemark(cancelRemark);
        zsyhServiceOrderService.updateZsyhServiceOrder(order);

        ZsyhServiceOrderLog log = new ZsyhServiceOrderLog();
        log.setId(IdWorker.getId());
        log.setOrderId(orderId);
        log.setLogType("1");
        log.setLogContent("师傅申请取消订单");
        zsyhServiceOrderLogService.insertZsyhServiceOrderLog(log);

        return success("成功");
    }

    /**
     * 维修师傅 订单中心 确认上门
     * @return
     * @throws Exception
     */
    @RequestMapping("/confirmServiceOrder")
    @ResponseBody
    public AjaxResult confirmServiceOrder(String orderId, String userId)throws Exception
    {
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setId(orderId);
        order.setOrderState("102");
        order.setTodoorTime(new Date());
        zsyhServiceOrderService.updateZsyhServiceOrder(order);

        ZsyhServiceOrderLog log = new ZsyhServiceOrderLog();
        log.setId(IdWorker.getId());
        log.setOrderId(orderId);
        log.setLogType("1");
        log.setLogContent("师傅确认上门");
        zsyhServiceOrderLogService.insertZsyhServiceOrderLog(log);
        return success("成功");
    }

    /**
     * 完成服务 -- 等待平台审核
     * @param orderId
     * @param userId
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @Transactional
    @RequestMapping("/finishServiceOrder")
    @ResponseBody
    public AjaxResult finishServiceOrder(String orderId, String userId, String orderImage, String orderRemarks)throws Exception
    {

        ZsyhAfterCommissionSetting setting = zsyhAfterCommissionSettingService.selectZsyhAfterCommissionSettingById(1L);



        ZsyhServiceOrderLog log = new ZsyhServiceOrderLog();
        log.setId(IdWorker.getId());
        log.setOrderId(orderId);
        log.setLogType("1");
        log.setLogContent("师傅提交完成服务");
        zsyhServiceOrderLogService.insertZsyhServiceOrderLog(log);

        //工单完成 合计费用
        ZsyhServiceOrder order = zsyhServiceOrderService.selectZsyhServiceOrderById(orderId);
        String repairId = order.getRepairStoreId();
        String factoryId = order.getFactoryId();
        BigDecimal orderMoneyAllFactory = BigDecimal.ZERO;
        BigDecimal orderMoneyAllRepair = BigDecimal.ZERO;
        BigDecimal orderMoneyFactory = order.getOrderMoneyFactory();
        BigDecimal orderMoneyRepair = order.getOrderMoneyRepair();
        BigDecimal repairCheckMoney = order.getRepairCheckMoney();//检测费


        orderMoneyAllFactory = orderMoneyAllFactory.add(orderMoneyFactory);
        orderMoneyAllRepair = orderMoneyAllRepair.add(orderMoneyRepair);

        //远程费
        Map applymoney1 = zsyhServiceOrderApplymoneyService.getApplyMoneyByOrderIdType(orderId, "1");
        //旧配件返件物流费
        Map applymoney2 = zsyhServiceOrderApplymoneyService.getApplyMoneyByOrderIdType(orderId, "2");
        //自购件费用
        Map partsMoney = zsyhServiceOrderPartsRelService.getPartsRelMoneyByOrderId(orderId);

        //师傅的费用 --人工费/服务费
        ZsyhServiceOrderSettlement settlement = new ZsyhServiceOrderSettlement();
        settlement.setOrderId(orderId);
        settlement.setRepairId(repairId);
        settlement.setFactoryId(factoryId);
        settlement.setFeeType("2");//费用分类 1厂家 2师傅
        if(orderMoneyRepair.doubleValue() > 0){
            settlement.setId(IdWorker.getId());
            settlement.setFeeClassify("1");//费用类型 1人工费/服务费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
            settlement.setFeeMoney(orderMoneyRepair);
            zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlement);
        }

        //师傅的费用 --检测费
        if(repairCheckMoney.doubleValue() > 0){
            settlement.setId(IdWorker.getId());
            settlement.setFeeClassify("7");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值) 7检测费/上门费
            settlement.setFeeMoney(repairCheckMoney);
            zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlement);
        }


        //师傅的费用 --里程费
        if(applymoney1 != null){
            BigDecimal applyMoneyAll = new BigDecimal(String.valueOf(applymoney1.get("applyMoneyAll")));
            if(applyMoneyAll.doubleValue() > 0){
                settlement.setId(IdWorker.getId());
                settlement.setFeeClassify("2");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
                settlement.setFeeMoney(applyMoneyAll);
                zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlement);
                orderMoneyAllRepair = orderMoneyAllRepair.add(applyMoneyAll);
            }
        }
        //师傅的费用 --返件物流费
        if(applymoney2 != null){
            BigDecimal applyMoneyAll = new BigDecimal(String.valueOf(applymoney2.get("applyMoneyAll")));
            if(applyMoneyAll.doubleValue() > 0){
                settlement.setId(IdWorker.getId());
                settlement.setFeeClassify("3");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
                settlement.setFeeMoney(applyMoneyAll);
                zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlement);
                orderMoneyAllRepair = orderMoneyAllRepair.add(applyMoneyAll);
            }
        }
        //师傅的费用 --自购配件费
        if(partsMoney != null){
            BigDecimal partsMoneyAllRepair = new BigDecimal(String.valueOf(partsMoney.get("partsMoneyAllRepair")));
            if(partsMoneyAllRepair.doubleValue() > 0){
                settlement.setId(IdWorker.getId());
                settlement.setFeeClassify("4");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
                settlement.setFeeMoney(partsMoneyAllRepair);
                zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlement);
                orderMoneyAllRepair = orderMoneyAllRepair.add(partsMoneyAllRepair);
            }
        }
        //师傅的费用 --保费
        if(setting != null){
            BigDecimal premiumMoney = new BigDecimal(setting.getPremiumMoney());
            settlement.setId(IdWorker.getId());
            settlement.setFeeClassify("6");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
            settlement.setFeeMoney(premiumMoney.negate());
            zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlement);
            orderMoneyAllRepair = orderMoneyAllRepair.subtract(premiumMoney);
        }

        //厂家的费用 --人工费/服务费
        ZsyhServiceOrderSettlement settlementFactory = new ZsyhServiceOrderSettlement();
        settlementFactory.setOrderId(orderId);
        settlementFactory.setRepairId(repairId);
        settlementFactory.setFactoryId(factoryId);
        settlementFactory.setFeeType("1");//费用分类 1厂家 2师傅

        if(orderMoneyFactory.doubleValue() > 0){
            settlementFactory.setId(IdWorker.getId());
            settlementFactory.setFeeClassify("1");//费用类型 1人工费/服务费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
            settlementFactory.setFeeMoney(orderMoneyFactory);
            zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlementFactory);
        }

        //厂家的费用 --检测费
        if(repairCheckMoney.doubleValue() > 0){
            settlementFactory.setId(IdWorker.getId());
            settlementFactory.setFeeClassify("7");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值) 7检测费/上门费
            settlementFactory.setFeeMoney(repairCheckMoney);
            zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlementFactory);
        }

        //厂家的费用 --里程费
        if(applymoney1 != null){
            BigDecimal applyMoneyAll = new BigDecimal(String.valueOf(applymoney1.get("applyMoneyAll")));
            if(applyMoneyAll.doubleValue() > 0){
                settlementFactory.setId(IdWorker.getId());
                settlementFactory.setFeeClassify("2");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
                settlementFactory.setFeeMoney(applyMoneyAll);
                zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlementFactory);
                orderMoneyAllFactory = orderMoneyAllFactory.add(applyMoneyAll);
            }
        }
        //厂家的费用 --返件物流费
        if(applymoney2 != null){
            BigDecimal applyMoneyAll = new BigDecimal(String.valueOf(applymoney2.get("applyMoneyAll")));
            if(applyMoneyAll.doubleValue() > 0){
                settlementFactory.setId(IdWorker.getId());
                settlementFactory.setFeeClassify("3");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
                settlementFactory.setFeeMoney(applyMoneyAll);
                zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlementFactory);
                orderMoneyAllFactory = orderMoneyAllFactory.add(applyMoneyAll);
            }
        }
        //厂家的费用 --自购配件费
        if(partsMoney != null){
            BigDecimal partsMoneyAllFactory = new BigDecimal(String.valueOf(partsMoney.get("partsMoneyAllFactory")));
            if(partsMoneyAllFactory.doubleValue() > 0){
                settlementFactory.setId(IdWorker.getId());
                settlementFactory.setFeeClassify("4");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
                settlementFactory.setFeeMoney(partsMoneyAllFactory);
                zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlementFactory);
                orderMoneyAllFactory = orderMoneyAllFactory.add(partsMoneyAllFactory);
            }
        }
        //厂家的费用 --平台费
        if(setting != null){
            BigDecimal platformCommission = new BigDecimal(setting.getPlatformCommission());
            settlementFactory.setId(IdWorker.getId());
            settlementFactory.setFeeClassify("5");//费用类型 1人工费 2里程费 3返件物流费 4自购件费用 5平台费 6保险费(负值)
            settlementFactory.setFeeMoney(platformCommission);
            zsyhServiceOrderSettlementService.insertZsyhServiceOrderSettlement(settlementFactory);
            orderMoneyAllFactory = orderMoneyAllFactory.add(platformCommission);
        }

        ZsyhServiceOrder _order = new ZsyhServiceOrder();
        _order.setId(orderId);
        _order.setOrderState("201");
        _order.setOrderImage(orderImage);
        _order.setOrderRemarks(orderRemarks);
        _order.setCompleteTime(new Date());
        _order.setOrderMoneyallFactory(orderMoneyAllFactory.setScale(2, BigDecimal.ROUND_HALF_UP));
        _order.setOrderMoneyallRepair(orderMoneyAllRepair.setScale(2, BigDecimal.ROUND_HALF_UP));
        zsyhServiceOrderService.updateZsyhServiceOrder(_order);

        return success("成功");
    }

    /**
     * 维修师傅 订单中心 费用信息
     * @return
     */
    @RequestMapping("/getServiceOrderMoneyDetails")
    @ResponseBody
    public AjaxResult getServiceOrderMoneyDetails(String userId, String orderId)throws Exception
    {
        //主订单
        ZsyhServiceOrder order = zsyhServiceOrderService.selectZsyhServiceOrderById(orderId);

        //工单费用信息
        ZsyhServiceOrderSettlement settlement = new ZsyhServiceOrderSettlement();
        settlement.setOrderId(orderId);
        settlement.setFeeType("2");
        List<ZsyhServiceOrderSettlement> settlementList = zsyhServiceOrderSettlementService.selectZsyhServiceOrderSettlementList(settlement);

        Map map = new HashMap();
        map.put("orderMoneyallRepair", order.getOrderMoneyallRepair());
        map.put("settlementList", settlementList);

        return success("成功", map);
    }

    /**
     * 维修师傅 订单中心 工单结算
     * @return
     * @throws Exception
     */
    @RequestMapping("/settlementServiceOrder")
    @ResponseBody
    public AjaxResult settlementServiceOrder(String orderId, String userId)throws Exception
    {
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setId(orderId);
        order.setOrderState("203");
        order.setSettlementTime(new Date());
        zsyhServiceOrderService.updateZsyhServiceOrder(order);

        ZsyhServiceOrderLog log = new ZsyhServiceOrderLog();
        log.setId(IdWorker.getId());
        log.setOrderId(orderId);
        log.setLogType("1");
        log.setLogContent("师傅申请工单结算");
        zsyhServiceOrderLogService.insertZsyhServiceOrderLog(log);
        return success("成功");
    }

    /**
     * 维修师傅 个人中心 已结算金额 待结算金额
     * @return
     */
    @RequestMapping("/getServiceOrderAllMoney")
    @ResponseBody
    public AjaxResult getServiceOrderAllMoney(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id

        //orderState 订单状态 0未接单（厂家下单）1已接单 101已预约 102服务中 2已完成 201平台待审核 202厂家待审核 203待拨款 3已支付 4已评价 5已取消 501师傅申请取消 6系统自动取消
        Map orderList1 = zsyhServiceOrderService.getMoneyAllList(repairId, null,"203");//待结算工单金额

        Map orderList2 = zsyhServiceOrderService.getMoneyAllList(repairId, null,"3");//已结算工单金额

        Map map = new HashedMap();
        map.put("moneyAllRepairNot", orderList1.get("moneyAllRepair"));
        map.put("moneyAllRepair", orderList2.get("moneyAllRepair"));

        return success("成功", map);
    }

}
